<?php
class Page{
	private $cfg = array(
			'currentPage' => 1,
			'itemPerPage' => 15,
			'totalItems' => 0,
			'range' => 5,
			);	
	public $totalPage;
	
	public function __construct( $cfg="" ){
		if( is_array($cfg) ) $this->config($cfg);
	}

	private function config( $cfg ){
		if( is_array($cfg) ){
			$this->cfg = array_merge($this->cfg,$cfg);
		}
	}

	public function init( $cfg="" ){
		if( is_array($cfg) ) $this->config($cfg);
		if( $this->cfg['itemPerPage']>=$this->cfg['totalItems'] ){
			return false;
		}
		$return = array();
		$return['pages'] = ceil($this->cfg['totalItems']/$this->cfg['itemPerPage']);
		$start = ($this->cfg['currentPage']-$this->cfg['range'])>1 ? $this->cfg['currentPage']-$this->cfg['range'] : 1;
		$end = ($return['pages']-$this->cfg['currentPage']) > $this->cfg['range'] ? $this->cfg['currentPage']+$this->cfg['range'] : $return['pages'];
		for( $i=1,$j=$start; $j<=$end; $i++,$j++ ){
			$return[$i]['page'] = $j;
			if( $j==$this->cfg['currentPage'] ) $return[$i]['index'] = 1;
		}
		return $return;
	}

}

